ESP32/ESP8266 PWM with MicroPython – Dim LED 您所在的位置:网站首页 esp32 esp8266 pwm ESP32/ESP8266 PWM with MicroPython – Dim LED

ESP32/ESP8266 PWM with MicroPython – Dim LED

2023-04-04 10:11| 来源: 网络整理| 查看: 265

ESP32/ESP8266 PWM with MicroPython – Dim LED Posted byGnd_To_VccApril 14, 2020April 16, 2020Posted inUncategorized

This tutorial shows how to generate PWM signals with the ESP32 and ESP8266 boards using MicroPython firmware. As an example, we’ll dim the brightness of an LED by changing the duty cycle over time.

Prerequisites

To follow this tutorial you need to have MicroPython firmware installed in your ESP32 or ESP8266 boards. You also need an IDE to write and upload the code to your board. We suggest using Thonny IDE or uPyCraft IDE:

Thonny IDE:Installing and getting started with Thonny IDEFlashing MicroPython Firmware with esptool.pyuPyCraft IDE:Install uPyCraft IDE (Windows, Mac OS X, Linux)Flash/Upload MicroPython Firmware to ESP32 and ESP8266 Schematic

For this example, wire an LED to your ESP board. We’ll wire the LED to GPIO 5 in both boards, but you can choose another suitable PWM pin. See the best pins to use in our ESP32 Pinout Reference Guide.

Parts required

Here’s a list of the parts you need to build the circuit:

ESP32 or ESP8266 (read: ESP32 vs ESP8266)5mm LED330 Ohm resistorBreadboardJumper wires Schematic – ESP32

Follow the next schematic diagram if you’re using an ESP32 board:

Schematic – ESP8266

Follow the next schematic diagram if you’re using an ESP8266 board:

ESP8266 PWM with MicroPython circuit to fade Dim LED

https://tpc.googlesyndication.com/safeframe/1-0-37/html/container.html

Script

Here’s the script that changes the LED brightness over time by increasing the duty cycle. This script works with the ESP32 and ESP8266.

# welcome to Gnd_To_Vcc!! from machine import Pin, PWM from time import sleep frequency = 5000 led = PWM(Pin(5), frequency) while True: for duty_cycle in range(0, 1024): led.duty(duty_cycle) sleep(0.005)

How the code works

To create a PWM pin, import the PWM class in addition to the Pin class from the machine module.

from machine import Pin, PWM

Then, create a PWM object called led.

led = PWM(Pin(5), frequency)

To create a PWM object, you need to pass as parameters, the pin it is connected to, the signal frequency and the duty cycle.

Frequency: The frequency can be a value between 0 and 78125. A frequency of 5000 Hz can be used to control the LED brightness.Duty cycle: The duty cycle can be a value between 0 and 1023. In which 1023 corresponds to 100% duty cycle (full brightness), and 0 corresponds to 0% duty cycle (unlit LED).

We’ll just set the duty cycle on the while loop, so we don’t need to pass the duty cycle parameter. If you don’t set the duty cycle when instantiating the PWM object, it will be 0 by default.

To set the duty cycle use the duty() method on the PWM object and pass the duty cycle as an argument:

led.duty(duty_cycle)

Inside the while loop, we create a for loop that increases the duty cycle by 1 in each loop with an interval of 5 ms between each change.

for duty_cycle in range(0, 1024): led.duty(duty_cycle) sleep(0.005)

The range() function has the following syntax:

range(start, stop, step) Start: a number that specifies at which position to start. We want to start with 0 duty cycle;Stop: a number that specifies at which position we want to stop, excluding that value. The maximum duty cycle is 1023, because we are incrementing 1 in each loop, the last value should be 1023+1. So, we’ll use 1024.Step: an integer number that specifies the incrementation. By default, incrementation is 1.

In each for loop, we set the LED’s duty cycle to the current duty_cycle value:

led.duty(duty_cycle)

After that, the duty_cycle variable is incremented by 1.

Demonstration

Save the code to your ESP board using Thonny IDE or uPyCraft IDE. The LED attached to GPIO 5 should increase brightness over time.Report this ad

Wrapping Up

If you like MicroPython, you may also like:

ESP32/ESP8266 GPIOs Explained with MicroPythonESP32/ESP8266 Digital Inputs and Digital Outputs with MicroPythonESP32/ESP8266 Analog Readings with MicroPythonESP32/ESP8266 MicroPython Web Server – Control Outputs

Thanks for reading.

AdvertisementShare this:TwitterFacebookLike this:Like Loading... Related Posted byGnd_To_VccApril 14, 2020April 16, 2020Posted inUncategorized Published by Gnd_To_Vcc

Here to spread my knowledge . Knowledge should always be spread not stored. View more posts



【本文地址】

公司简介

联系我们

今日新闻

    推荐新闻

    专题文章
      CopyRight 2018-2019 实验室设备网 版权所有